﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Media;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Windows.Media.Imaging;
using System.Reflection;
using Jbe.Composite.Extensions;
using System.Windows.Input;
using Jbe.TestSuite.LogViewer.Domain;
using Jbe.TestSuite.LogViewer.Properties;

namespace Jbe.TestSuite.LogViewer.Applications
{
    internal class LogViewPresentationModel : PresentationModel<ILogView>, IHeaderInfoProvider
    {
        private readonly DelegateCommand startCommand;
        private readonly DelegateCommand stopCommand;
        private readonly DelegateCommand clearCommand;
        private bool recorderEnabled;


        public LogViewPresentationModel(ILogView view)
            : base(view)
        {
            recorderEnabled = LogRecorder.Enabled;
            startCommand = new DelegateCommand(Start, CanStart);
            stopCommand = new DelegateCommand(Stop, CanStop);
            clearCommand = new DelegateCommand(Clear);
        }


        public string HeaderInfo
        {
            get { return Resources.LogViewer; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/EventLog.ico")); }
        }

        public ICommand StartCommand { get { return startCommand; } }

        public ICommand StopCommand { get { return stopCommand; } }

        public ICommand ClearCommand { get { return clearCommand; } }

        public IList<LogEntry> LogEntries
        {
            get { return LogRecorder.LogEntries; }
        }

        private bool RecorderEnabled
        {
            get { return recorderEnabled; }
            set
            {
                if (recorderEnabled != value)
                {
                    recorderEnabled = value;
                    LogRecorder.Enabled = value;
                    startCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
            }
        }


        private bool CanStart() { return !RecorderEnabled; }

        private void Start() { RecorderEnabled = true; }

        private bool CanStop() { return RecorderEnabled; }

        private void Stop() { RecorderEnabled = false; }

        private void Clear() { LogRecorder.Clear(); }
    }
}
